package pms.theme.service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow.MissingCellPolicy;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import pms.common.dao.MasterDao;
import pms.common.dao.ProgressDao;
import pms.common.dao.ProjectDao;
import pms.common.dao.ReviewDao;
import pms.common.dao.TargetDao;
import pms.common.dao.ThemeDao;
import pms.common.utils.Consts;
import pms.common.utils.DateUtil;
import pms.common.value.MasterValue;
import pms.common.value.ProgressValue;
import pms.common.value.ProjectValue;
import pms.common.value.ReviewReportValue;
import pms.common.value.ReviewValue;
import pms.common.value.TargetValue;
import pms.common.value.ThemeValue;
import pms.common.value.UserSessionValue;

public class ThemeServiceImpl implements ThemeService {

	private ReviewDao reviewDao;
	
	private MasterDao masterDao;
	
	public MasterDao getMasterDao() {
		return masterDao;
	}

	public void setMasterDao(MasterDao masterDao) {
		this.masterDao = masterDao;
	}

	public ReviewDao getReviewDao() {
		return reviewDao;
	}

	public void setReviewDao(ReviewDao reviewDao) {
		this.reviewDao = reviewDao;
	}

	private ProgressDao progressDao;
	
	public ProgressDao getProgressDao() {
		return progressDao;
	}

	public void setProgressDao(ProgressDao progressDao) {
		this.progressDao = progressDao;
	}

	private TargetDao targetDao;
	
	public TargetDao getTargetDao() {
		return targetDao;
	}

	public void setTargetDao(TargetDao targetDao) {
		this.targetDao = targetDao;
	}

	private ProjectDao projectDao;
	
	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	
	private ThemeDao themeDao;
	

	public ThemeDao getThemeDao() {
		return themeDao;
	}

	public void setThemeDao(ThemeDao themeDao) {
		this.themeDao = themeDao;
	}

	public List<ProjectValue> getProjectList() {
		
		return projectDao.getProjectList();
	}

	public List<ThemeValue> getThemeList(int projectId) {
		return themeDao.getThemeList(projectId);
	}

	public void updateTheme(ThemeValue value) {
		themeDao.updateTheme(value);
		
	}

	public void insertTheme(ThemeValue value) {
		themeDao.insertTheme(value);
		
	}

	public void deleteTheme(int themeId, int userId) {
		themeDao.deleteTheme(themeId, userId);
		
	}

	public List<TargetValue> getTargetList(int themeId) {
		return targetDao.getTargetList(themeId);
	}

	public void deleteTarget(int targetId, int userId) {
		System.out.println("kakakaka");
		targetDao.deleteTarget(targetId, userId);
		
	}

	public void insertTarget(TargetValue value) {
		targetDao.insertTarget(value);
		
	}

	public void updateTarget(TargetValue value) {
		targetDao.updateTarget(value);
		
	}

	public List<ProgressValue> getProgressList(int targetId) {
		return progressDao.getProgressList(targetId);
		
	}

	public void insertProgress(ProgressValue value) {
		progressDao.InsertProgress(value);
		
		if(value.getPgsValue()==100) {
			//進捗が100%になると、該当作業のステータスが「レビュー中(10)」にする。
			int targetId = value.getPgsTargetId();
			targetDao.updateTargetStatus(targetId, Consts.TARGET_STATUS_レビュー中);
		} else if(value.getPgsValue()<100 && value.getPgsValue()>0) {
			//進捗が100%になると、該当作業のステータスが「作業中(9)」にする。
			int targetId = value.getPgsTargetId();
			targetDao.updateTargetStatus(targetId, Consts.TARGET_STATUS_作業中);
		} else if(value.getPgsValue()<=0) {
			//進捗が100%になると、該当作業のステータスが「未着手(8)」にする。
			int targetId = value.getPgsTargetId();
			targetDao.updateTargetStatus(targetId, Consts.TARGET_STATUS_未着手);
		}
	}

	public List<ReviewValue> getReviewList(int targetId, String targetName, List<UserSessionValue> userList) {
		List<ReviewValue> result = reviewDao.getReviewList(targetId);
		for(ReviewValue rv : result) {
			rv.setReviewTargetName(targetName);
			for(UserSessionValue usv : userList) {
				if(rv.getReviewUserId()==usv.getUserId()) {
					rv.setReviewUserName(usv.getUserDispName());
				}
				if(rv.getReviewSolveUserId()==usv.getUserId()) {
					rv.setReviewSolveUserName(usv.getUserDispName());
				}
				if(rv.getReviewAdmitUserId()==usv.getUserId()) {
					rv.setReviewAdmitUserName(usv.getUserDispName());
				}
			}
		}
		return result;
	}

	public void insertReview(ReviewValue value) {
		value.setReviewAdmitUserId(-1);
		reviewDao.insertReview(value);
		
	}

	public void updateReview(ReviewValue value) {
		reviewDao.updateReview(value);
		
	}

	public void admitReview(int userId, int themeId, int targetId, int reviewId) {
		reviewDao.admitReview(userId, themeId, targetId, reviewId);
		
	}

	public void deleteReview(int themeId, int targetId, int reviewId) {
		reviewDao.deleteReview(themeId, targetId, reviewId);
		
	}

	public void reviewExport(int themeId,  List<UserSessionValue> userList, List<TargetValue> targetList, String themeName, String projectName) {
		List<ReviewValue> reviewList = reviewDao.getReviewListByTheme(themeId);
		List<MasterValue> master1 = masterDao.getMaster("review_type");
		for(ReviewValue rv : reviewList) {
			for(TargetValue tv : targetList) {
				if(rv.getReviewTargetId() == tv.getTargetId()) {
					rv.setReviewTargetName(tv.getTargetName());
					break;
				}
			}
			for(MasterValue mv : master1) {
//				System.out.println(rv.getReviewType());
//				System.out.println(mv.getCode() + mv.getValue());
				if(rv.getReviewType() == mv.getCode()) {
					rv.setReviewTypeName(mv.getValue());
				}
			}
			for(UserSessionValue usv : userList) {
				if(rv.getReviewUserId()==usv.getUserId()) {
					rv.setReviewUserName(usv.getUserDispName());
				}
				if(rv.getReviewSolveUserId()==usv.getUserId()) {
					rv.setReviewSolveUserName(usv.getUserDispName());
				}
				if(rv.getReviewAdmitUserId()==usv.getUserId()) {
					rv.setReviewAdmitUserName(usv.getUserDispName());
				}
			}
		}
//		for(ReviewValue rv : reviewList) {
//			System.out.println(rv.getReviewUserId());
//			System.out.println(rv.getReviewUserName());
//		}
		try {
			
			POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream("c:/pms/template/review.xls"));
			HSSFWorkbook wb = new HSSFWorkbook(poifs);
			
			HSSFCellStyle cellStyleLeft = wb.createCellStyle();
			cellStyleLeft.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
			cellStyleLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);
			cellStyleLeft.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);
			cellStyleLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			cellStyleLeft.setWrapText(true);
			
			HSSFCellStyle cellStyleRight = wb.createCellStyle();
			cellStyleRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			cellStyleRight.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
			cellStyleRight.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);
			cellStyleRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			cellStyleRight.setWrapText(true);
			
			HSSFCellStyle cellStyle = wb.createCellStyle();
			cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
			cellStyle.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);
			cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			cellStyle.setWrapText(true);
			
			HSSFCellStyle cellStyleLeftLast = wb.createCellStyle();
			cellStyleLeftLast.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
			cellStyleLeftLast.setBorderRight(HSSFCellStyle.BORDER_THIN);
			cellStyleLeftLast.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
			cellStyleLeftLast.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			cellStyleLeftLast.setWrapText(true);
			
			HSSFCellStyle cellStyleRightLast = wb.createCellStyle();
			cellStyleRightLast.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			cellStyleRightLast.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
			cellStyleRightLast.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
			cellStyleRightLast.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			cellStyleRightLast.setWrapText(true);
			
			HSSFCellStyle cellStyleLast = wb.createCellStyle();
			cellStyleLast.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			cellStyleLast.setBorderRight(HSSFCellStyle.BORDER_THIN);
			cellStyleLast.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
			cellStyleLast.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			cellStyleLast.setWrapText(true);

			
			
			//記録表シート
			HSSFSheet sheet1 = wb.getSheetAt(0);
			//プロジェクト名(E2)
			HSSFRow row1 = sheet1.getRow(1);
			row1.getCell(4,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(projectName));
			//テーマ名（F2）
			row1.getCell(5,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(themeName));
			//作成者（H2）--「NIT」固定
			row1.getCell(7,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString("NIT"));
			//作成日（I2）
			row1.getCell(8,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(DateUtil.getCurrentDate()));
			
			//記録詳細
			Map<String, ReviewReportValue> rrvMap = new HashMap<String, ReviewReportValue>();
			for(ReviewValue rv : reviewList) {
				String key = String.valueOf(rv.getReviewCycle())+String.valueOf(rv.getReviewTargetId());
				ReviewReportValue rrv = rrvMap.get(key);
				if(rrv==null) {
					rrv = new ReviewReportValue();
					rrv.setComment("");
					rrv.setCount(1);
					rrv.setCycle(rv.getReviewCycle()+"");
					rrv.setDate(rv.getReviewCreateDate());
					rrv.setName(rv.getReviewTargetName());
					rrv.setUser(rv.getReviewUserName());
					rrvMap.put(key, rrv);
				} else {
					rrv.addCount();
				}
			}
			HSSFRow lastRow = null;
			int rowCount = 7;
			int noCount = 1;
			for(ReviewReportValue rrv : rrvMap.values()) {
				HSSFRow row2 = sheet1.getRow(rowCount++);
				row2.setHeightInPoints(60);
				row2.getCell(0,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(noCount++);
				row2.getCell(0).setCellStyle(cellStyleLeft);
				row2.getCell(1,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rrv.getDate()));
				row2.getCell(1).setCellStyle(cellStyle);
				row2.getCell(2,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rrv.getUser()));
				row2.getCell(2).setCellStyle(cellStyle);
				row2.getCell(3,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rrv.getCycle()));
				row2.getCell(3).setCellStyle(cellStyle);
				row2.getCell(4,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rrv.getName()));
				row2.getCell(4).setCellStyle(cellStyle);
				row2.getCell(5,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(rrv.getCount());
				row2.getCell(5).setCellStyle(cellStyle);
				row2.getCell(6,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rrv.getStatus()));
				row2.getCell(6).setCellStyle(cellStyle);
				row2.getCell(7,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rrv.getComment()));
				row2.getCell(7).setCellStyle(cellStyle);
				row2.getCell(8).setCellStyle(cellStyle);
				row2.getCell(9,HSSFRow.CREATE_NULL_AS_BLANK).setCellStyle(cellStyleRight);
				lastRow = row2;
			}
			if(lastRow !=null) {
				lastRow.getCell(0).setCellStyle(cellStyleLeftLast);
				lastRow.getCell(1).setCellStyle(cellStyleLast);
				lastRow.getCell(2).setCellStyle(cellStyleLast);
				lastRow.getCell(3).setCellStyle(cellStyleLast);
				lastRow.getCell(4).setCellStyle(cellStyleLast);
				lastRow.getCell(5).setCellStyle(cellStyleLast);
				lastRow.getCell(6).setCellStyle(cellStyleLast);
				lastRow.getCell(7).setCellStyle(cellStyleLast);
				lastRow.getCell(8).setCellStyle(cellStyleLast);
				lastRow.getCell(9).setCellStyle(cellStyleRightLast);
			}
			//指摘内容シート
			HSSFSheet sheet2 = wb.getSheetAt(1);
			HSSFRow row2 = sheet2.getRow(1);
			row2.getCell(6,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(projectName));
			//テーマ名（F2）
			row2.getCell(7,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(themeName));
			//作成者（H2）--「NIT」固定
			row2.getCell(9,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString("NIT"));
			//作成日（I2）
			row2.getCell(11,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(DateUtil.getCurrentDate()));
			
			noCount = 1;
			rowCount = 7;
	
			
			lastRow = null;
			for(ReviewValue rv : reviewList) {
				System.out.println("row:" + rowCount);
				HSSFRow row3 = sheet2.getRow(rowCount++);
				row3.setHeightInPoints(60);
				row3.getCell(0,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(noCount++);
				row3.getCell(0).setCellStyle(cellStyleLeft);
				row3.getCell(1,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewCreateDate()));
				row3.getCell(1).setCellStyle(cellStyle);
				row3.getCell(2,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewUserName()));
				row3.getCell(2).setCellStyle(cellStyle);
				row3.getCell(3,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(rv.getReviewCycle());
				row3.getCell(3).setCellStyle(cellStyle);
				row3.getCell(4,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewTargetName()));
				row3.getCell(4).setCellStyle(cellStyle);
				row3.getCell(5,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewTypeName()));
				row3.getCell(5).setCellStyle(cellStyle);
				row3.getCell(6,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewDetail()));
				row3.getCell(6).setCellStyle(cellStyle);
				row3.getCell(7,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewSolveDetail()));
				row3.getCell(7).setCellStyle(cellStyle);
				row3.getCell(8,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewSolveUserName()));
				row3.getCell(8).setCellStyle(cellStyle);
				row3.getCell(9,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewExpectDate()));
				row3.getCell(9).setCellStyle(cellStyle);
				row3.getCell(10,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewSolveDate()));
				row3.getCell(10).setCellStyle(cellStyle);
				row3.getCell(11,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewAdmitUserName()));
				row3.getCell(11).setCellStyle(cellStyle);
				row3.getCell(12,HSSFRow.CREATE_NULL_AS_BLANK).setCellValue(new HSSFRichTextString(rv.getReviewAdmitDate()));
				row3.getCell(12).setCellStyle(cellStyleRight);
				lastRow = row3;
			}
			if(lastRow !=null) {
				lastRow.getCell(0).setCellStyle(cellStyleLeftLast);
				lastRow.getCell(1).setCellStyle(cellStyleLast);
				lastRow.getCell(2).setCellStyle(cellStyleLast);
				lastRow.getCell(3).setCellStyle(cellStyleLast);
				lastRow.getCell(4).setCellStyle(cellStyleLast);
				lastRow.getCell(5).setCellStyle(cellStyleLast);
				lastRow.getCell(6).setCellStyle(cellStyleLast);
				lastRow.getCell(7).setCellStyle(cellStyleLast);
				lastRow.getCell(8).setCellStyle(cellStyleLast);
				lastRow.getCell(9).setCellStyle(cellStyleLast);
				lastRow.getCell(10).setCellStyle(cellStyleLast);
				lastRow.getCell(11).setCellStyle(cellStyleLast);
				lastRow.getCell(12).setCellStyle(cellStyleRightLast);
			}
			
			
			
			//出力
			FileOutputStream   fileOut   =     
				  new   FileOutputStream("c:/pms/download/review"+DateUtil.getCurrentTimestampCompact()+".xls");   
			wb.write(fileOut); 
			

			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

}
